import java.util.Scanner;

public class Main {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();//重量
        String b = in.next();//是否加急
        double total = 0.0;
        if (a <= 1) {
            //1kg以内
            total = 20;
        } else {
            total = 20 + (a - 1) + 1;
        }
        if (b.equals("y")) {
            total += 5;
        }
        System.out.println((int) total);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        String[] strs = new String[n];
        int minLen = -1;
        int i = 0;
        while (in.hasNext()) {
            String tmp = in.next();
            strs[i] = tmp;
            i++;
        }
        for (int j = 0; j < n; j++) {
            if (strs[j].equals(str1)) {
                for (int k = j + 1; k < n; k++) {
                    if (strs[k].equals(str2)) {
                        if (minLen == -1 || k - j < minLen) {
                            minLen = k - j;
                        }
                    }
                }
            }
            if (strs[j].equals(str2)) {
                for (int k = j + 1; k < n; k++) {
                    if (strs[k].equals(str1)) {
                        if (minLen == -1 || k - j < minLen) {
                            minLen = k - j;
                        }
                    }
                }
            }
        }
        System.out.println(minLen);
    }
}